package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/findMenuList")
    public Result findMenuList(String username){
        try {
            List<Map<String,Object>> menuList = menuService.findMenuList(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findPage(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu,Integer[] roleIds){
        try {
            menuService.add(menu, roleIds);
            return new Result(true,"菜单添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单添加失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"查询成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu,Integer[] roleIds){
        try {
            menuService.edit(menu,roleIds);
            return new Result(true,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

    @RequestMapping("/findOneMenu")
    public Result findOneMenu(){
        try {
            List<Map<String, String>> oneMenuList = menuService.findOneMenu();
            return new Result(true,"查询成功",oneMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
}
